package it.mauriziobasile.dietarapida.gui;

import it.mauriziobasile.dietarapida.Alimento;
import it.mauriziobasile.dietarapida.DietaRapidaApp;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 * 
 * @author Maurizio Basile
 */
public class InsertAlimento extends javax.swing.JFrame {

    private static final long serialVersionUID = -7124917983432458437L;

    private Properties properties;

    int lastSelected = 0;

    private DietaRapidaView parent;

    public InsertAlimento(DietaRapidaView par) {
        parent = par;
        initComponents();
        properties = parent.getProperties();
        String[] allnutri = properties.getProperty("tuttecolonne").split(";");
        for (int i = 0; i < allnutri.length; i++) {
            ((DefaultTableModel) jTable1.getModel()).addRow(new Object[] { allnutri[i],""});
        }
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }

    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonInserisci = new javax.swing.JButton();
        setName("Form");
        jScrollPane1.setName("jScrollPane1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Nutriente", "Valore" }) {

            private static final long serialVersionUID = 8206895832074934259L;
            Class<?>[] types = new Class[] { java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, true };

            public Class<?> getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jTable1.setName("jTable1");
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setHeaderValue("Valore");
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("Nutriente");

        jButtonInserisci.setText("Inserisci");
        jButtonInserisci.setName("jButtonInserisci");
        jButtonInserisci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButtonInsertActionPerformed(evt);
                } catch (SQLException e) {
                    DietaRapidaApp.logger.severe("Problemi nell'inserire il nuovo alimento "+e.getMessage());
                }
            }
        });

        BorderLayout layout = new BorderLayout();
        setLayout(layout);

        add(jScrollPane1,BorderLayout.CENTER);
        add(jButtonInserisci,BorderLayout.PAGE_END);
        setPreferredSize(new Dimension(400,500));
        setMinimumSize(new Dimension(400,500));
        pack();
    }

    private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {
        Hashtable<String, String> alimento = new Hashtable<String, String>();
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            alimento.put((String)jTable1.getValueAt(i, 0), (String)jTable1.getValueAt(i, 1));
        }
        parent.getTabellaAlim().insertAlimento(new Alimento(alimento));
        this.setVisible(false);
    }


    private javax.swing.JButton jButtonInserisci;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;

}
